package client.ui;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import phasebook.client.msgs.UserProfileInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;
import client.ui.UserListSection.Mode;

import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.TabBarControls;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.CloseClickHandler;
import com.smartgwt.client.widgets.tab.events.TabCloseClickEvent;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class RootPanel extends HLayout  implements ResponseHandler, CloseClickHandler{
	private TabSet tabSet;
	private ImagePicker picker;
	private UserFinder search;
	private EditProfile settings;
	private Controller controller;
	private UserProfileInfo myProfile;

	public RootPanel(Controller controller, UserProfileInfo userProfile) {
		this.controller = controller;
		this.myProfile = userProfile;
		super.setWidth100();
		super.setHeight100();
		
		search = new UserFinder(controller);
		picker = new ImagePicker(controller);
		settings = new EditProfile(controller, userProfile);

		SectionStack leftStack = new SectionStack();
		leftStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		leftStack.setWidth("20%");
		leftStack.setHeight100();
		leftStack.setShowResizeBar(true);
		leftStack.setAnimateSections(true);
		leftStack.addSection(new UserListSection(controller, Mode.Friends));
		leftStack.addSection(new UserListSection(controller, Mode.Requests));
		leftStack.addSection(new UserListSection(controller, Mode.MyRequests));
		leftStack.addSection(new LotterySection(controller));
		
		
		ToolStrip toolStrip = new ToolStrip();
		toolStrip.setHeight100();
		toolStrip.setWidth("1%");
		
		ToolStripButton imagesButton = new ToolStripButton("Album");
		imagesButton.setIcon("[SKIN]/actions/color_swatch.png");
		toolStrip.addButton(imagesButton);
		imagesButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				picker.show();
			}
		});
		
		ToolStripButton searchButton = new ToolStripButton("Search");
		searchButton.setIcon("[SKIN]/actions/search.png");
		toolStrip.addButton(searchButton);
		searchButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				search.show();
			}
		});
		
		ToolStripButton settingsButton = new ToolStripButton("Settings");
		settingsButton.setIcon("[SKIN]/actions/configure.png");
		toolStrip.addButton(settingsButton);
		settingsButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				settings.show();
			}
		});		
		tabSet = new TabSet();
		tabSet.setTabBarPosition(Side.TOP);
		tabSet.setTabBarThickness(25);
		tabSet.setTabBarAlign(Side.LEFT);
		tabSet.setWidth100();
		tabSet.setHeight100();
		tabSet.setAnimateTabScrolling(true);
		tabSet.setTabBarControls(TabBarControls.TAB_SCROLLER,TabBarControls.TAB_PICKER, toolStrip);
		tabSet.addCloseClickHandler(this);
		
		super.addMember(leftStack);
		super.addMember(tabSet);
		
		controller.addResponseHandler(this);
	}
	
	private HashMap<Long,UserTab> userTabs = new HashMap<Long, UserTab>(); 

	@Override
	public void onResponse(CombinedResponse response) {
		List<UserProfileInfo> profiles = response.getProfiles();
		if (profiles!=null){
			for(UserProfileInfo profile:profiles){
				UserTab userTab = userTabs.get(profile.getUserId());
				if (userTab==null){
					userTab = new UserTab(controller, profile, profile.getUserId()==myProfile.getUserId(),picker);
					userTabs.put(profile.getUserId(), userTab);
					tabSet.addTab(userTab);
					tabSet.selectTab(userTab);
				}
			}
		}
	}

	@Override
	public void onCloseClick(TabCloseClickEvent event) {
		userTabs.remove(((UserTab)event.getTab()).close());
	}
}
